import apiClient from '@/api/apiClient.ts';
import { Account, AccountWithId, AssignRoleParams, UserInfo } from '#/entity.ts';
import { Page } from '#/api.ts';

export enum AccountApi {
  GetList = '/roadCure/user/getPage',
  GetSingle = '/roadCure/user',
  Create = '/roadCure/user/save',
  Update = '/roadCure/user/update',
  Delete = '/roadCure/user/delete',
  ResetPwd = '/roadCure/user/resetPassword',
  AssignRole = '/roadCure/multi/assignRole',
}

export interface AccountListSearchParams {
  parentId: number;
  phone?: string;
  realName?: string;
  roleId?: number;
  userName?: string;
}

const getAccountList = (data: AccountListSearchParams & Page) =>
  apiClient.post<UserInfo[]>({
    url: AccountApi.GetList,
    data,
  });

const getAccountById = (userId: string | number) =>
  apiClient.get<UserInfo>({ url: `${AccountApi.GetSingle}/${userId}` });

const create = (data: Account) =>
  apiClient.post<AccountWithId>({
    url: AccountApi.Create,
    data,
  });

const update = (data: AccountWithId) =>
  apiClient.put<AccountWithId>({
    url: AccountApi.Update,
    data,
  });

const remove = (id: number) =>
  apiClient.delete<boolean>({
    url: `${AccountApi.Delete}/${id}`,
  });

const resetPwd = (id: string) =>
  apiClient.put<boolean>({
    url: AccountApi.ResetPwd,
    params: new URLSearchParams({ id }),
  });

const assignRole = (data: AssignRoleParams) =>
  apiClient.post<boolean>({
    url: AccountApi.AssignRole,
    data,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });

export default {
  getAccountList,
  getAccountById,
  create,
  update,
  remove,
  resetPwd,
  assignRole,
};
